@import '../core/style/private';
@import '../core/theming/palette';
@import '../core/theming/theming';
@import '../core/theming/private';
@import '../core/typography/typography-utils';


$mat-datepicker-selected-today-box-shadow-width: 1px;
$mat-datepicker-selected-fade-amount: 0.6;
$mat-datepicker-range-fade-amount: 0.2;
$mat-datepicker-today-fade-amount: 0.2;
$mat-calendar-body-font-size: 13px !default;
$mat-calendar-weekday-table-font-size: 11px !default;

@mixin _mat-datepicker-color($palette) {
  @include mat-date-range-colors(
    mat-color($palette, default, $mat-datepicker-range-fade-amount));

  .mat-calendar-body-selected {
    background-color: mat-color($palette);
    color: mat-color($palette, default-contrast);
  }

  .mat-calendar-body-disabled > .mat-calendar-body-selected {
    $background: mat-color($palette);

    @if (type-of($background) == color) {
      background-color: fade-out($background, $mat-datepicker-selected-fade-amount);
    }
    @else {
      // If we couldn't resolve to background to a color (e.g. it's a CSS variable),
      // fall back to fading the content out via `opacity`.
      opacity: $mat-datepicker-today-fade-amount;
    }
  }

  .mat-calendar-body-today.mat-calendar-body-selected {
    box-shadow: inset 0 0 0 $mat-datepicker-selected-today-box-shadow-width
                mat-color($palette, default-contrast);
  }
}

// Utility mixin to target cells that aren't selected. Used to make selector easier to follow.
@mixin _mat-datepicker-unselected-cell {
  &:not(.mat-calendar-body-selected):not(.mat-calendar-body-comparison-identical) {
    @content;
  }
}

@mixin mat-datepicker-color($config-or-theme) {
  $config: mat-get-color-config($config-or-theme);
  $foreground: map-get($config, foreground);
  $background: map-get($config, background);
  $disabled-color: mat-color($foreground, disabled-text);

  .mat-calendar-arrow {
    border-top-color: mat-color($foreground, icon);
  }

  // The prev/next buttons need a bit more specificity to
  // avoid being overwritten by the .mat-icon-button.
  .mat-datepicker-toggle,
  .mat-datepicker-content .mat-calendar-next-button,
  .mat-datepicker-content .mat-calendar-previous-button {
    color: mat-color($foreground, icon);
  }

  .mat-calendar-table-header {
    color: mat-color($foreground, hint-text);
  }

  .mat-calendar-table-header-divider::after {
    background: mat-color($foreground, divider);
  }

  .mat-calendar-body-label {
    color: mat-color($foreground, secondary-text);
  }

  .mat-calendar-body-cell-content,
  .mat-date-range-input-separator {
    color: mat-color($foreground, text);
    border-color: transparent;
  }

  .mat-calendar-body-disabled > .mat-calendar-body-cell-content {
    @include _mat-datepicker-unselected-cell {
      color: $disabled-color;
    }
  }

  .mat-form-field-disabled .mat-date-range-input-separator {
    color: $disabled-color;
  }

  .mat-calendar-body-cell:not(.mat-calendar-body-disabled):hover,
  .cdk-keyboard-focused .mat-calendar-body-active,
  .cdk-program-focused .mat-calendar-body-active {
    & > .mat-calendar-body-cell-content {
      @include _mat-datepicker-unselected-cell {
        background-color: mat-color($background, hover);
      }
    }
  }

  .mat-calendar-body-in-preview {
    $divider-color: mat-color($foreground, divider);

    @if type-of($divider-color) == color {
      // The divider color is set under the assumption that it'll be used
      // for a solid border, but because we're using a dashed border for the
      // preview range, we need to bump its opacity to ensure that it's visible.
      color: rgba($divider-color, min(opacity($divider-color) * 2, 1));
    }
    @else {
      color: $divider-color;
    }
  }

  .mat-calendar-body-today {
    @include _mat-datepicker-unselected-cell {
      // Note: though it's not text, the border is a hint about the fact that this is today's date,
      // so we use the hint color.
      border-color: mat-color($foreground, hint-text);
    }
  }

  .mat-calendar-body-disabled > .mat-calendar-body-today {
    @include _mat-datepicker-unselected-cell {
      $color: mat-color($foreground, hint-text);

      @if (type-of($color) == color) {
        border-color: fade-out($color, $mat-datepicker-today-fade-amount);
      }
      @else {
        // If the color didn't resolve to a color value, but something like a CSS variable, we can't
        // fade it out so we fall back to reducing the element opacity. Note that we don't use the
        // $mat-datepicker-today-fade-amount, because hint text usually has some opacity applied
        // to it already and we don't want them to stack on top of each other.
        opacity: 0.5;
      }
    }
  }

  @include _mat-datepicker-color(map-get($config, primary));

  .mat-datepicker-content {
    @include mat-private-theme-elevation(4, $config);
    background-color: mat-color($background, card);
    color: mat-color($foreground, text);

    &.mat-accent {
      @include _mat-datepicker-color(map-get($config, accent));
    }

    &.mat-warn {
      @include _mat-datepicker-color(map-get($config, warn));
    }
  }

  .mat-datepicker-content-touch {
    @include mat-private-theme-elevation(0, $config);
  }

  .mat-datepicker-toggle-active {
    color: mat-color(map-get($config, primary), text);

    &.mat-accent {
      color: mat-color(map-get($config, accent), text);
    }

    &.mat-warn {
      color: mat-color(map-get($config, warn), text);
    }
  }

  .mat-date-range-input-inner[disabled] {
    color: mat-color($foreground, disabled-text);
  }
}

@mixin mat-datepicker-typography($config-or-theme) {
  $config: mat-get-typography-config($config-or-theme);
  .mat-calendar {
    font-family: mat-font-family($config);
  }

  .mat-calendar-body {
    font-size: $mat-calendar-body-font-size;
  }

  .mat-calendar-body-label,
  .mat-calendar-period-button {
    font: {
      size: mat-font-size($config, button);
      weight: mat-font-weight($config, button);
    }
  }

  .mat-calendar-table-header th {
    font: {
      size: $mat-calendar-weekday-table-font-size;
      weight: mat-font-weight($config, body-1);
    }
  }
}

@mixin mat-date-range-colors(
  $range-color,
  $comparison-color: rgba(#f9ab00, $mat-datepicker-range-fade-amount),
  $overlap-color: #a8dab5,
  $overlap-selected-color: darken($overlap-color, 30%)) {

  .mat-calendar-body-in-range::before {
    background: $range-color;
  }

  .mat-calendar-body-comparison-identical,
  .mat-calendar-body-in-comparison-range::before {
    background: $comparison-color;
  }

  .mat-calendar-body-comparison-bridge-start::before,
  [dir='rtl'] .mat-calendar-body-comparison-bridge-end::before {
    background: linear-gradient(to right, $range-color 50%, $comparison-color 50%);
  }

  .mat-calendar-body-comparison-bridge-end::before,
  [dir='rtl'] .mat-calendar-body-comparison-bridge-start::before {
    background: linear-gradient(to left, $range-color 50%, $comparison-color 50%);
  }

  .mat-calendar-body-in-range > .mat-calendar-body-comparison-identical,
  .mat-calendar-body-in-comparison-range.mat-calendar-body-in-range::after {
    background: $overlap-color;
  }

  .mat-calendar-body-comparison-identical.mat-calendar-body-selected,
  .mat-calendar-body-in-comparison-range > .mat-calendar-body-selected {
    background: $overlap-selected-color;
  }
}

@mixin _mat-datepicker-density($config-or-theme) {}

@mixin mat-datepicker-theme($theme-or-color-config) {
  $theme: mat-private-legacy-get-theme($theme-or-color-config);
  @include mat-private-check-duplicate-theme-styles($theme, 'mat-datepicker') {
    $color: mat-get-color-config($theme);
    $density: mat-get-density-config($theme);
    $typography: mat-get-typography-config($theme);

    @if $color != null {
      @include mat-datepicker-color($color);
    }
    @if $density != null {
      @include _mat-datepicker-density($density);
    }
    @if $typography != null {
      @include mat-datepicker-typography($typography);
    }
  }
}
